﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models;

namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class NewsController : Controller
    {
        public ActionResult Index()
        {
            List<NewsType> titleList = NewsTypeService.getAllNew();
            ViewBag.A = titleList;
            return View();
        }
        public ActionResult DelLogin(int page, int limit, string title, string isPub,string type)
        {
            string sql = string.Format("SELECT COUNT(*) FROM [dbo].[News]");
            int cou = (int)DBHelp.ExcuteScalar(sql);
            List<News> list = NewsService.getArtList(page, limit, title, isPub, type);
            return Json(new
            {
                code = 0,
                msg = "请求成功",
                count = cou,
                data = list
            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add()
        {
            List<NewsType> news = NewsService.NewsTypet();
            ViewBag.A = news;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddDel(News art)
        {
            int n = NewsService.AddNews(art);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功", });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败", });
            }
        }
        public ActionResult DelArt(string Id)
        {
            int n = NewsService.DelNews(Id);
            if (n > 0)
            {
                return Json(new { success = true, msg = "删除成功", });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败", });
            }

        }
        public ActionResult Edit(string Id)
        {   
            News art = NewsService.GetById(Id);
            List<NewsType> news = NewsService.NewsTypet();
            ViewBag.A = art;
            ViewBag.B = news;
            return View();
        }
 
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(News art)
        {
            int n = NewsService.EditNews(art);
            if (n > 0)
            {
                return Json(new { success = true, msg = "编辑成功", });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败", });
            }
        }
        [HttpPost]
        public ActionResult UploadPhoto()
        {
            HttpPostedFileBase postFile = Request.Files["uplaodImg"];
            if (postFile != null)
            {
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";              
                string fullUpLoadPath = Server.MapPath("~/images/");
                postFile.SaveAs(fullUpLoadPath + newFileName);
                return Json(new { success = true, path = "images/" + newFileName, msg = "上传成功" });
            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }
        }
        [HttpPost]
        public ActionResult UploadLayedit2()
        {
            HttpPostedFileBase postFile = Request.Files["file"];
            if (postFile != null)
            {
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                string uploadPath = "~/images/";
                string fullUploadPath = Server.MapPath(uploadPath);
                postFile.SaveAs(fullUploadPath + newFileName);
                return Json(new { code = 0, msg = "上传成功", data = new { src = "/images/" + newFileName } });
            }
            else
            {
                return Json(new { code = -1, msg = "失败", data = new { src = "" } });
            }
        }
    }
}